﻿package net.wangit.dev.enumeration;

import net.wangit.MapList;
import net.wangit.Unit;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionContextHelper;
import net.wangit.dev.ConsoleDomainUtil;
import net.wangit.dev.LockUtil;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.util.DBHelper;
import net.wangit.support.UnitInterceptor;
import net.wangit.util.Checker;
import net.wangit.view.element.component.SelectElement;
import net.wangit.view.enumeration.EnumerationFactory;
import net.wangit.view.unit.UnitComponent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class EnumerationUI implements UnitInterceptor {

	private static Log log = LogFactory
			.getLog(net.wangit.dev.enumeration.EnumerationUI.class);
	public static final String KEY = "net.wangit.dev.enumeration.EnumerationUI.eid";
	private static final String CSS_INVALID = "invalid";
	private static final String SQL_CONFIG = "select * from ENUMCONFIG where enumid=?";
	private static final String SQL_ENUMERATION = "select * from ENUM where enumid=?";

	public EnumerationUI() {
	}

	public String intercept(ActionContext ac, Unit unit) throws Exception {
		String domain = ConsoleDomainUtil.getDomain(ac);
		String source = ac.getRequestParameter("src");
		boolean copy = !Checker.isEmpty(source);
		String eid = copy ? source : ActionContextHelper.getRequest2Session(ac,
				"eid", EnumerationUI.KEY, null);
		String unitShowMode = unit.getShowMode();
		if (!"a".equals(unitShowMode)) {
			StringBuffer title = new StringBuffer(((UnitComponent) unit)
					.getTitle());
			title.append(" <span style='font-weight:normal'>( ").append(
					getPreview(ac, domain, eid)).append(" )</span>");
			unit.setTitle(title.toString());
		}
		Unit el = (Unit) unit.getElement("el").getObject();
		if (!copy && "a".equals(unitShowMode)) {
			String groupId = ac.getRequestParameter("groupid");
			unit.getElement("gid").setDefaultValue(groupId);
		} else if (unit.getData() == null) {
			DB db = DBHelper.getDomainDB(domain);
			MapList configData = db.query(EnumerationUI.SQL_CONFIG, eid, 2);
			MapList enumerationData = db.query(EnumerationUI.SQL_ENUMERATION,
					eid, 2);
			unit.setData(configData);
			setItemData(el, enumerationData);
			if (copy) {
				configData.put(0, "enumid", "");
				configData.put(0, "l", "0");
			}
		}
		LockUtil.setElements(ac, unit, new Object[][] {
				new Object[] { unit, "save" },
				new Object[] { el, "add", "remove" } });
		return unit.write(ac);
	}

	private void setItemData(Unit itemUnit, MapList data) {
		itemUnit.setData(data);
		if (!Checker.isEmpty(data)) {
			for (int i = 0; i < data.size(); i++) {
				int valid = data.getRow(i).getInt("v", 1);
				if (valid == 0)
					itemUnit.setRowCss(i, EnumerationUI.CSS_INVALID);
			}

		}
	}

	private String getPreview(ActionContext ac, String domain,
			String enumerationId) {
		MapList list = null;
		try {
			list = EnumerationFactory.getEnumeration(ac, domain, enumerationId);
		} catch (Exception e) {
			log.error("Enumeration preview failed:", e);
		} catch (Throwable t) {
			log.error("Enumeration preview failed:", t);
		}
		if (!Checker.isEmpty(list)) {
			StringBuffer html = new StringBuffer();
			SelectElement preview = new SelectElement("", list);
			preview.write(ac, html, -1, null);
			return html.toString();
		} else {
			return "<label class=error>failed!</label>";
		}
	}

}
